import { request } from 'umi';

import { getBaseUrl } from '@/services/urlconst';

import { getTenant } from '@/utils/authority';

let baseUrl = getBaseUrl();

/* 获取字典树 */
export const getDicTypeList = async () => {
  let url = `${baseUrl}/dictionaryTypeController/getDicTypeList`;
  try {
    return request(url, {
      method: 'GET',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/* 获取字典列表信息 */
export const getDicListByTypeID = async (id: string) => {
  let url = `${baseUrl}/dictionaryController/getDicListByTypeID/${id}`;
  try {
    return request(url, {
      method: 'GET',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/* 是否启用数据字典 */
export const isOpenDictionary = async (params: any) => {
  let url = `${baseUrl}/dictionaryController/isOpen`;
  try {
    return request(url, {
      method: 'PUT',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/* 是否显示数据字典 */
export const isDisplayDictionary = async (params: any) => {
  let url = `${baseUrl}/dictionaryController/isDisplay`;
  try {
    return request(url, {
      method: 'PUT',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/*新增字典类型*/
export const addDictionaryType = async (params: Record<string, any>) => {
  let url = `${baseUrl}/dictionaryTypeController/add`;
  try {
    return request(url, {
      method: 'POST',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/*编辑数据字典类型*/
export const editDictionaryType = async (params: Record<string, any>) => {
  let url = `${baseUrl}/dictionaryTypeController/edit`;
  try {
    return request(url, {
      method: 'PUT',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/*新增字典*/
export const addDictionary = async (params: Record<string, any>) => {
  let url = `${baseUrl}/dictionaryController/add`;
  try {
    return request(url, {
      method: 'POST',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/* 编辑数据字典 */
export const editDictionary = async (params: Record<string, any>) => {
  let url = `${baseUrl}/dictionaryController/edit`;
  try {
    return request(url, {
      method: 'PUT',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};
